<%@ page import="agentFree2.*,java.net.*,java.io.IOException,java.io.InputStream,java.io.DataInputStream,java.io.BufferedInputStream,java.util.Date,java.util.logging.Logger,java.util.regex.Pattern,java.util.regex.Matcher,javax.jdo.PersistenceManager,javax.servlet.http.*" %>
<%
	//Stage1. Parsing Url Source and getting two phone numbers.

	Pattern agentBlock = Pattern
			.compile("<span class=\"smallred\">.{18}</span> <strong>.{0,100}</strong><br><span class=phonetext>.{7,20}</span>(, <span class=phonetext>.{7,20}</span>){0,3}    </div>");
	Pattern phoneNumber = Pattern.compile("[+375,8]*[ ,(]*...?[ ,)]*[0-9]{7}");
	URL u = new URL("http://www.hata.by/rent-flat/");
	String s;
	s = ReaderFactory.readUrlData(u,"ISO-8859-1");
	//out.println(s);
	Matcher agentsMatcher = agentBlock.matcher(s);
	int count = 0;
    //OPen connection to datastore.
	Date date = new Date();
	String url = "hata.by";
	Agent newAgent;
	PersistenceManager pm = PMF.get().getPersistenceManager();
	
	while (agentsMatcher.find()) {
		count++;
		//System.out.println("!!"+count);

		String content = agentsMatcher.group();
		System.out.println(content);
		String contentData = content.replaceAll("[a-z,A-Z,0-9,<,>,/,\\+,=,\"]*","");
		Matcher phoneMatcher = phoneNumber.matcher(content);

		while(phoneMatcher.find()){
		try{
			String phone = phoneMatcher.group();			
			phone = phone.replaceFirst("^\\+375|^80","");
			phone = phone.replaceAll("[ ,(,)]*","");
			//System.out.println("phone"+phone);
			Long phoneNumberParsed = Long.parseLong(phone);
		
		
			//Stage2. Writing two phones to datastore.
		
			newAgent =  new Agent(contentData, phoneNumberParsed, date, url);
			out.println("newAgent  "+newAgent.getPhoneNumber()+"        "+newAgent.getAgentName());
			pm.makePersistent(newAgent);
			

		}catch(Exception e)
		{out.print(e);	
			
		}	
		
		}
		out.println("!!"+count);
	}
	
	pm.close();
	
%>